﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.AutoOrder_Fuji.IView;
using MINERP.MODEL.AutoOrder_Fuji;
using MINERP.AutoOrder_Fuji.Presenter;

namespace MINERP.AutoOrder_Fuji
{
    public partial class FrmAutoMain : Form, IMainView
    {
        private MainvPresenter _presenter;

        public FrmAutoMain()
        {
            InitializeComponent();
            //MessageBox.Show("开始处始化pretenis");
            _presenter = new MainvPresenter(this);
            barEditItem1.EditValue = "等待下单";
        }

        #region IMainView 成员
        public delegate void DLrefreshDisPlay(List<AutoOrderFolder_Info> listFolder);

        public void DisPlayFolder(List<AutoOrderFolder_Info> listFolder)
        {
            if (gridControl1.InvokeRequired)
            {
                this.Invoke(new DLrefreshDisPlay(DisPlayFolder), new object[] { listFolder });
                return;
            }
            gridControl1.DataSource = listFolder;
            gridControl1.RefreshDataSource();
            lblCount.Caption = "总数：" + listFolder.Count;
        }

        public delegate void DLBindStatusList(List<AutoOrderStatus> listFolder);
        public void BindStatusCmbList(List<AutoOrderStatus> list)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new DLBindStatusList(BindStatusCmbList), new object[] { list });
                return;
            }
            repositoryItemComboBox2.Items.Clear();
            foreach (var status in list)
            {
                repositoryItemComboBox2.Items.Add(status);
            }
        }

        public delegate void rereshDataGrid();
        public void refreshDataGrid()
        {
            if (gridControl1.InvokeRequired)
            {
                this.Invoke(new rereshDataGrid(refreshDataGrid), new object[] { });
                return;
            }
            btnResumeErr.Enabled = false;
            if (barEditItem1.EditValue == null)
                return;

            string selectValue = barEditItem1.EditValue.ToString();
            AutoOrderStatus selectStatus = AutoOrderStatus.WaitDownLoad | AutoOrderStatus.DownLoading | AutoOrderStatus.Resolving;
            switch (selectValue)
            {
                case "错误": selectStatus = AutoOrderStatus.Error;
                    btnResumeErr.Enabled = true;
                    break;
                case "完成": selectStatus = AutoOrderStatus.Successed;
                    break;
                default: selectStatus = AutoOrderStatus.WaitDownLoad;
                    break;
            }

            _presenter.ShowGridView(selectStatus);

        }

        #endregion

        private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                switch (((AutoOrderFolder_Info)gridView1.GetRow(e.RowHandle)).Status)
                {
                    case AutoOrderStatus.WaitDownLoad: e.Appearance.BackColor = Color.White;
                        break;
                    case AutoOrderStatus.DownLoading:
                    case AutoOrderStatus.Resolving: e.Appearance.BackColor = Color.Pink;
                        break;
                    case AutoOrderStatus.Successed: e.Appearance.BackColor = Color.YellowGreen;
                        break;
                    case AutoOrderStatus.Error: e.Appearance.BackColor = Color.Tomato;
                        break;
                    default:
                        break;
                }
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Convert.ToBoolean(barButtonItem2.Tag))//关闭
            {
                barButtonItem2.Enabled = false;
                barButtonItem2.Caption = "正在关闭服务...";
                _presenter.StopAutoWork();
                barButtonItem2.Enabled = true;
                barButtonItem2.Caption = "开启解析服务";
                barButtonItem2.Tag = false;
            }
            else//开启操作
            {
                barButtonItem2.Enabled = false;
                barButtonItem2.Caption = "正在开启服务...";
                _presenter.StartAutoWork();
                barButtonItem2.Enabled = true;
                barButtonItem2.Caption = "关闭解析服务";
                barButtonItem2.Tag = true;
            }
        }

        private void barEditItem1_EditValueChanged(object sender, EventArgs e)
        {
            refreshDataGrid();
        }

        private void btnResumeErr_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<AutoOrderFolder_Info> selectedOrder = new List<AutoOrderFolder_Info>();
            foreach (var index in gridView1.GetSelectedRows())
            {
                selectedOrder.Add((AutoOrderFolder_Info)gridView1.GetRow(index));
            }
            if (selectedOrder.Count > 0)
            {
                _presenter.ResumeErr(selectedOrder);
            }

        }

        /// <summary>
        /// 清空缓存中的List
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClearOld_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _presenter.ClearListAndRefresh();
        }

        private void btnOpenMinitorFolder_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _presenter.OpenMonitorFolder();
        }

        private void FrmAutoMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("这是自动开单服务程序，是否关闭？", "询问", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                e.Cancel = true;
            }
        }

        


    }
}
